"""
A class to exercise the JT core functions.
"""
import vtk
import JT
import Calibration
from numpy import array, zeros, asarray, degrees

def Test():
    """
    Test the volume rendering implemented in JT
    """
#===============================================================================
#    Data Paths
#===============================================================================
    inputVolFileName = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.mha'
    maskFileName = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1b2VOI.mha'
    staFile = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.sta'
    calFile = 'C:/Users/bryan/bryan-code/trunk/TestData/ext_cal1.txt'
    startPositionsPath = 'C:/Users/bryan/bryan-code/2D3D/startpositions_200_ordered.txt'
    
    #===============================================================================
    #    Generate Lateral Projection
    #===============================================================================
    xraycam = JT.XRayCamera()
    drr = JT.DRR()
    
    drr.SetXRayCamera(xraycam)
    drr.SetVolumeFilename(inputVolFileName)
    drr.SetBlackOnWhiteImage()
    drr.InteractionOff()
    
    #Set Mask
    drr._volume.SetImageMask(maskFileName)
    
    #===============================================================================
    #    Calibration
    #===============================================================================
    cal = Calibration.ExternalCalibration()
    cal.LoadConsolidatedCalibration(calFile)
    cal.LoadStaFile(staFile)
    
    xraycam.SetExternalCalibration(cal)
    
    vtkMatrix = cal.ConvertNumpyToVTKArray(cal._VolumeT)
    drr._volume.SetUserMatrix(vtkMatrix)
    drr._volume.SetOriginalTransform(cal._VolumeT)
    drr._volume.SetBodyCenter((3.906, 2.17, -7.378))
    
    #use volume calibration data to position volume back to null
    #position from starting postion
    #this will demonstrate the interaction between SetPosition, SetOrigin, and 
    #SetOrientation functions with the SetUserMatrix function.
    #
    #drr._volume.SetOrigin(3.906, 2.17, -7.378)

#===============================================================================
#    Read Start Positions
#===============================================================================
    f = open(startPositionsPath)
    # Start positions are 200, 6 element poses, plus error
    startPoses = zeros((201,7))

    for ii, line in enumerate(f):
        startPoses[ii,:] = asarray(line.split(),'f')
    
    for ii,pose in enumerate(startPoses):
        #Transform volume using start pose given in file
        volumePose = drr._volume.SetPose(pose[0:6])
        drr.Render()
        
        #Transform volume back to original pose using inverse of start pose
        #drr._volume.SetOrientation(-degrees(pose[3:6]))
        #drr._volume.AddPosition(-pose[0:3])
        
        drr.Render()
        drr.SaveGeneratedImage(
            'C:/Users/bryan/bryan-code/trunk/Images/StartPoses/null/startpose_%i.png' % (ii))

if __name__ == "__main__":
    Test()